package com.moranyunxi.www.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

public class Util {
    /**
     * 压缩上传视频
     *
     * @param httpUrl 链接
     */
    public static String compressionAndUploadVideo(String httpUrl) throws IOException {
        String webShowPath = "";
        try {
            /*String absUrl= getRedirectUrl(httpUrl);
            URL url = new URL(absUrl);*/

            URL url = new URL(httpUrl);

            // 获取链接
            URLConnection conn = url.openConnection();

            // 输入流
            File targetFile = null;
            File tmpFile = null;// 临时文件，用于存放压缩后的视频
            try (InputStream inStream = conn.getInputStream()) {
                // 临时文件
                targetFile = File.createTempFile(UUID.randomUUID().toString(), "tmp");
                java.nio.file.Files.copy(inStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

                tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".mp4");

                // 视频压缩
                //File compressionFile = MultimediaCompressionUtil.compressionVideo(targetFile, tmpFile);

                String fileName = UUID.randomUUID().toString() + ".mp4";

                // 保存路径
                //String formatDate = DateUtils.getFormatDate("yyyy-MM-dd", DateUtils.getCurDate());
                //String dirName = TENCENT_VIDEO_OSS_DIR + "/" + formatDate + "/";
//                dirName = "test/" + userId + "/" + formatDate + "/";

                // 视频路径
                //webShowPath = TENCENT_VIDEO_OSS_ACCESS_PREFIX + dirName + fileName;
                System.out.println("\n-----视频保存OSS路径-----\n" + webShowPath);

                //AliyunOssClientUtil.uploadFileByLocalFile(compressionFile, dirName + fileName);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //在JVM退出时删除文件。也可以调用file.delete();立即删除文件。
                targetFile.deleteOnExit();
                // 删除临时文件
                targetFile.delete();

                //在JVM退出时删除文件。也可以调用file.delete();立即删除文件。
                tmpFile.deleteOnExit();
                // 删除临时文件
                tmpFile.delete();
            }
        } catch (FileNotFoundException e) {
            //log.error(e.getMessage());
        } catch (IOException e) {
            //log.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return webShowPath;
    }
}
